﻿

//* Copyright(C) 2005-2012, all rights reserved.
//*
//* The following code is copyrighted and contains proprietary information
/* and trade secrets of Foxit Software Inc.      
 **************************************************************************
 */

/**
 * 负责创建Annot
 */

define(function (require, exports, module) {
    require('./Annot.js');
    require('./MarkupAnnot.js');
    require('./LinkAnnot.js');
    require('./InkAnnot.js');
    require('./TypewriterAnnot.js');
    var PDFLinkAnnot = WebPDF.PDFData.PDFLinkAnnot;
    var PDFMarkupAnnot = WebPDF.PDFData.PDFMarkupAnnot;
    var PDFTypewriterAnnot = WebPDF.PDFData.PDFTypewriterAnnot;
    var AnnotDataJSONFormat = WebPDF.PDFData.AnnotDataJSONFormat;
    var PDFInkAnnot = WebPDF.PDFData.PDFInkAnnot;
    var AnnotType = WebPDF.PDFData.AnnotType;
    var annotNameMapper = {};

    WebPDF.AnnotFactory = (function () {
        var factory =
        {
            createAnnot: function (jsonData,isSourceAnnot) {
                try {
                    var annot = null;
                    switch (jsonData[AnnotDataJSONFormat.type]) {
                        case AnnotType.Link:
                        {
                            annot = new PDFLinkAnnot(jsonData);
                        }
                            break;
                        case AnnotType.TypeWriter:
                        {
                            annot = new PDFTypewriterAnnot(jsonData);
                        }
                        	break;
                        case AnnotType.MarkUp:
                        {
                        	switch (jsonData[WebPDF.PDFData.AnnotDataJSONFormat.subType])
							{
								case AnnotType.Ink:
								{
									annot = new PDFInkAnnot(jsonData);
								}break;
								default:
									annot = new PDFMarkupAnnot(jsonData);
								break;
							}
                            
                            var jsonAnnotGroup = jsonData[AnnotDataJSONFormat.group];
                            if (jsonAnnotGroup) {
                                for (var i = 0; i < jsonAnnotGroup.length; i++) {
                                    var childAnnot = this.createAnnot(jsonAnnotGroup[i],isSourceAnnot);
                                    if (childAnnot != null) {
                                        childAnnot.setHeadAnnot(annot);
                                        annot.addAnnotToGroup(childAnnot);
                                    }
                                }
                            }
                        }
                            break;
                        default: break;
                    }
                    if (annot != null) {
                        if(isSourceAnnot != null) {
                            annot.setSourceAnnot(isSourceAnnot);
                        }
                        
                        var uniqueId = WebPDF.Common.createUniqueId();
                        annot.setAnnotName(uniqueId);
                        annotNameMapper[uniqueId] = 1;
                    }
                    
                    return annot;
                }
                catch (ex) {
                    console.error(ex);
                }
            }
        };
        return factory;
    })();

    return WebPDF.AnnotFactory;
});